<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\TestApi;

use Carbon\Carbon;
use App\Models\Eloquent;

/**
 * Class QtpTestApi
 *
 * @property int $id
 * @property string $test_report_uid
 * @property int $test_module_id
 * @property int $api_id
 * @property string $api_name
 * @property string $api
 * @property string $path
 * @property string $domain
 * @property array|null $headers
 * @property string $method_type
 * @property int $test_param_sun
 * @property int $success_param_sun
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class TestApi extends Eloquent
{
	protected $table = 'test_apis';

	protected $casts = [
		'test_module_id' => 'int',
		'api_id' => 'int',
		'headers' => 'json',
		'test_param_sun' => 'int',
		'success_param_sun' => 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'test_report_uid',
		'test_module_id',
		'api_id',
		'api_name',
		'api',
		'path',
		'domain',
		'headers',
		'method_type',
		'test_param_sun',
		'success_param_sun',
		'status'
	];

    // 状态 0待测试 1通过 2不通过
    const STATUS_INIT = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_ERROR = 2;
    public static $status_maps = [
        self::STATUS_INIT => '待测试',
        self::STATUS_SUCCESS => '测试通过',
        self::STATUS_ERROR => '测试不通过'
    ];

    public function label()
    {
        $testReport = TestReport::getByUid($this->test_report_uid);
        $this->testModule = TestModule::find($this->test_module_id);
        $this->test_adminer = $testReport->test_adminer ?? '';
        $this->created_adminer = $testReport->created_adminer ?? '';
        $this->status_label = self::$status_maps[$this->status] ?? '';
        $this->tested_at = $testReport->tested_at ?? '';
        $this->test_validator_sum = TestValidatorRes::where('test_api_id', $this->id)->count();
        $this->success_validator_sum = TestValidatorRes::where('status', TestValidatorRes::STATUS_SUCCESS)->where('test_api_id', $this->id)->count();

        return $this;
    }

    public static $fillables = [
        'test_report_uid' => '测试报告唯一UUID',
        'test_module_id' => '测试模块ID',
        'api_id' => '原始接口ID',
        'api_name' => '接口名称',
        'api' => '原始Api',
        'path' => '接口路径',
        'domain' => '域名',
        'headers' => '执行headers',
        'method_type' => '请求方式',
        'test_param_sun' => '测试用例(参数)总数',
        'success_param_sun' => '测试成功用例(参数)总数',
        'status' => '状态',
    ];
}
